﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Responsibility
{

    public class LogChainDemo
    {
        public static Logger CreateLogger()
        {
            LoggerError logError = new LoggerError(LogLevelType.Error);
            LoggerDebug logDebug = new LoggerDebug(LogLevelType.Debug);
            LoggerInfo logInfo = new LoggerInfo(LogLevelType.Info);
            //设置实体中的指针，并返回等级最低的
            logInfo.SetNextLogger(logDebug);
            logDebug.SetNextLogger(logError);
            return logInfo;
        }
    }

    public abstract class Logger
    {
        private LogLevelType logType;

        public LogLevelType LogType
        {
            get { return logType; }
        }

        public Logger(LogLevelType logLevel)
        {
            this.logType = logLevel;
        }

        public Logger NextLogger { get; protected set; }

        public void SetNextLogger(Logger logger)
        {
            if (logger != null)
            {
                this.NextLogger = logger;
            }
        }
        public void LogMessage(LogLevelType leve, string str)
        {
            if (leve == logType)
            {
                Write(str);
            }
            else
            {
                if (this.NextLogger != null)
                {
                    this.NextLogger.LogMessage(leve, str);
                }
                else
                {
                    Console.WriteLine("无法获取的日志级别：{0}信息：{1}",leve,str);
                }
            }
        }
        public abstract void Write(string str);
    }

    public class LoggerInfo : Logger
    {
        public LoggerInfo(LogLevelType logLevel) : base(logLevel)
        {
        }

        public override void Write(string str)
        {
            Console.WriteLine("信息日志：{0}", str);
        }
    }

    public class LoggerDebug : Logger
    {
        public LoggerDebug(LogLevelType logLevel) : base(logLevel)
        {
        }

        public override void Write(string str)
        {
            Console.WriteLine("调试级别日志：{0}", str);
        }
    }


    public class LoggerError : Logger
    {
        public LoggerError(LogLevelType logLevel) : base(logLevel)
        {
        }

        public override void Write(string str)
        {
            Console.WriteLine("错误日志：{0}", str);
        }
    }

    public enum LogLevelType
    {
        Null,
        Info,
        Debug,
        Error,
    }
}
